package Ioc_DI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.Collections;

/**
 * 扫描路径，spring在扫描哪些类是交给spring来管理的，是以spring的启动类的位置为基准
 * 扫描所有的文件，比如spring的启动类在 Ioc_DI这个包下，那么他就会扫描这个包下所有的类
 * 如果需要修改扫描路径，就可以加上ComponentScan这个标签
 */

//这样他就只会扫描org.example.spring_frist.LibraryProject.controller这个路径下的类
//@ComponentScan("org.example.spring_frist.LibraryProject.controller")
@SpringBootApplication
public class SpringSecondApplication {


    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringSecondApplication.class, args);

    }
    public static void main6(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringSecondApplication.class, args);
        r2 g = (r2) context.getBean("r2");
        g.p();
        System.out.println(context.getBean("b4"));
    }
    /**
     * di
     */
    public static void main5(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringSecondApplication.class, args);
//        injectionLabel i = (injectionLabel) context.getBean(injectionLabel.class);
//        i.p();
        multipleBean bean = context.getBean(multipleBean.class);
        System.out.println(bean);
        getMu bean1 = context.getBean(getMu.class);
        bean1.retB5_6();
        multipleBean bean2 = context.getBean(multipleBean.class);
        System.out.println(bean2);
        helloDi b3 = (helloDi) context.getBean("b3");
        b3.p();
    }
    /**
     * 在component中使用bean在标识方法可以使方法也在bean容器中，这样就可以允许一个component类
     * 返回多个不同的对象 被bean修饰的方法的返回值会被注册为一个bean
     */
    public static void main4(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringSecondApplication.class, args);

        Components c1 = (Components) context.getBean("c1");
        System.out.println(c1.name);


        Components c2 = (Components) context.getBean("c2",Components.class);
        System.out.println(c2.name);

        Components c3 = (Components) context.getBean("c3",Components.class);
        System.out.println(c3.name);
    
    }
    /**
     * 获取对象的name在获取时需要遵循一定的规则，储存在bean中的对象一律使用小驼峰的格式，如果类名字前俩个字符为大写，那就保持原来的类名字储存
     * 比如 储存 HelloWorld 在bean中是 helloWorld  而储存NHello 在bean中是NHello
     */
    public static void main3(String[] args) {
        ApplicationContext context = SpringApplication.run(SpringSecondApplication.class, args);

        //有多种写法是为了确保获取的对象的准确性
        helloController h1 = (helloController) context.getBean("helloController");
        h1.p();
        helloController h2 = context.getBean(helloController.class);
        h2.p();
        helloController h3 = context.getBean("helloController", helloController.class);
        h3.p();
        System.out.println(h1);
        System.out.println(h2);
        System.out.println(h3);

        helloService s1 = context.getBean(helloService.class);
        s1.p();
        helloConfiguration c1 = context.getBean(helloConfiguration.class);
        c1.p();
        helloRepository r1 = context.getBean(helloRepository.class);
        r1.p();
    }

    //通过spring获取实例 启动springboot
    public static void main2(String[] args) {
        SpringApplication context = new SpringApplication(SpringSecondApplication.class);
        context.setDefaultProperties(Collections.singletonMap("server.port", "8082"));
        ApplicationContext app = context.run(args);
        ioc c = app.getBean(ioc.class);//获取了ioc的实例
        System.out.println(c.d1);
        c.d1.p();
    }

    public static void main1(String[] args) {
        SpringApplication s = new SpringApplication(SpringSecondApplication.class);
        s.setDefaultProperties(Collections.singletonMap("server.port", "8082"));
        s.run(args);
    }

    public static void main0(String[] args) {
        SpringApplication.run(SpringSecondApplication.class, args);
    }
}
